﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DTO;
using HRProject.Code.DAO;
using System.Collections.Generic;

namespace HRProject.Asset
{
    public partial class AssetSelection : System.Web.UI.Page
    {
        private BLAssetType root;
        private List<BLAsset> assets;
        private string assetTypeId;

        public List<BLAsset> Assets
        {
            get { return assets; }
            set { assets = value; }
        }

        public BLAssetType Root
        {
            get { return root; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            root = getRoot();

            assets = new List<BLAsset>();
            assetTypeId = Request.Params["assetTypeId"];
            if (assetTypeId != null)
            {
                try
                {
                    Guid assetTypeGuid = new Guid(assetTypeId);
                    AssetDAO dao = new AssetDAO();
                    BLAsset filter = new BLAsset();
                    filter.Id = Guid.Empty;
                    filter.Name = string.Empty;
                    filter.Type = new BLAssetType();
                    filter.Type.Id = assetTypeGuid;
                    assets = dao.search(filter);
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.StackTrace);
                }
            }
        }

        private BLAssetType getRoot()
        {
            AssetTypeDAO assetTypeDAO = new AssetTypeDAO();
            BLAssetType result = assetTypeDAO.getRootAssetType();
            return result;
        }

        public void printAssetTypeNode(BLAssetType assetType)
        {
            string pre = "<div class=\"line\">";
            string selected = string.Empty;
            if (assetType.Id.ToString().Equals(assetTypeId))
            {
                selected = " selected";
            }
            string line = "<div class=\"node"+selected+"\"><a href=\"javascript:void(0);\" onclick=\"viewAssetType(this);\">";
            line += assetType.Name + "</a>";
            line += "<input type=\"hidden\" name=\"typeId\" value=\""+assetType.Id+"\"/></div>";
            if (assetType.Parent != null)
            {
                line = "<div class=\"pad\"></div>" + line;
                line = "<div class=\"branch direct\"></div>" + line;
            }
            BLAssetType ancestor = assetType.Parent;
            while (ancestor != null)
            {
                if (ancestor.Parent != null)
                {
                    if (ancestor.Parent.Children.IndexOf(ancestor) < (ancestor.Parent.Children.Count - 1))
                    {
                        line = "<div class=\"tab\"></div>" + line;
                        line = "<div class=\"branch\"></div>" + line;
                    }
                    else
                    {
                        line = "<div class=\"tab\"></div>" + line;
                        line = "<div class=\"branch empty\"></div>" + line;
                    }
                }
                ancestor = ancestor.Parent;
            }
            string suf = "</div>";
            Response.Write(pre + line + suf);

            foreach (BLAssetType child in assetType.Children)
            {
                printAssetTypeNode(child);
            }
        }
    }
}
